﻿package org.display.util
{
	import flash.display.*;
	import flash.events.*;
	import flash.filesystem.*;
	import flash.net.*;
	import flash.utils.*;
	
	import mx.controls.Alert;
	
	import org.data.Global;
	import org.net.*;
	import org.utils.*;
	
	import mx.managers.CursorManager;


	public class UpdataLibrary
	{
		var paths:Array=["swf/org/display/component","xml/org/display/component",
			             "org/commands/component","org/abcFrame","org/data","org/enmu","org/net","org/utils",
						 "jsfl/org/display/component" ]
			
		var updataFiles:Array=[]	
			
		var filelist:String="http://www.wakao123.com/test/filelist.xml"
			
		var downurl:String="http://www.wakao123.com/test/readfile.php"
			
		public function UpdataLibrary()
		{
			
			//改写服务器配置文件
			var files:Array=[]
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){

				for each(var item in XML(e).item){
				
					files.push( item.toString())

				}
				
				//trace(files)
				
				files=ArrayControl.defoam(files,true)
					
				reExcept(files)
					
				downfile(updataFiles)
				
				
			
			}
			
			new AbcHTTPServices(filelist,res)
			
		}
		
		function downfile(files:Array){
			
			var arr:Array=files.shift().split("/")

			var downpath:String=downurl+"?path="+arr.join("/");
			
			arr.shift()
				
			arr.push(arr.pop().replace(/\.txt/gis,""))
			
			var savepath:String=Global.workspace.replace(/\\/gis,"/")+"/"+arr.join("/")

			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				var currentData:ByteArray = e as ByteArray;//得到下载的数据
				
				var file:File =File.desktopDirectory.resolvePath(savepath); 
				var fileStr:FileStream = new FileStream();
				fileStr.open(file, FileMode.WRITE);
				//fileStr.position = fileStr.bytesAvailable;//将指针指向文件尾
				fileStr.writeBytes(currentData, 0, currentData.length);//在文件中写入新下载的数据
				fileStr.close();//关闭文件流
				
				if(files.length>0){
					downfile(files)
				}else{
					Alert.show("公共库更新完毕！")
					CursorManager.removeAllCursors()
				}
				
				//trace(savepath)
				
			}
			
			//改写服务器配置文件
			new AbcLoaderBinary(downpath,res)
			
			CursorManager.setBusyCursor();

		}
		
		function reExcept(files:Array){

			var file:String=files.shift()
				
			for(var j=0;j<paths.length;j++){
						
				var check:String = paths[j]
					
				if(file.indexOf(check)!=-1){
							
					updataFiles.push(file)

					}
			}
			
			if(files.length>0){reExcept(files)}
			
		}
		
	}
}